﻿using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using Dm;

namespace Mrsoft.Password
{
    static class AutoRecoginze
    {

        //这个可以使用支援文件进行设置
        private const string color = "000d21-2fffff";
        private const string dict_name = @".\system\dm.pass.txt";

        public static SecretCard RecoginzeFromImage(string fileName) {
            using (Image image = Image.FromFile(fileName)) {
                fileName = @".\dm_image.bmp";
                image.Save(fileName, ImageFormat.Bmp);
            }

            //创建为临时文件 ~tmp.bmp
            Dm.Idmsoft dm = new dmsoft();
            dm.SetDict(0, dict_name);
            var str = dm.OcrInFile(0, 0, 2000, 2000, fileName, color, 1);

            var idx = str.IndexOf("4000");
            str = str.Substring(idx);
            var id = str.Substring(0, 16);
            var list = new List<Password>();

            var index = 16;
            for (char column = 'A'; column < 'H'; column++) {
                for (var row = 1; row < 8; row++) {
                    Password password = new Password {
                        SerialId = id,
                        ColumnId = column.ToString(),
                        RowId = row.ToString(),
                        KeyValue = str.Substring(index, 2)
                    };
                    list.Add(password);
                    index += 2;
                }
            }

            return new SecretCard(id, list);
        }

    }
}
